package com.hsf.cloudweb.intercepeter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "crosFilter",urlPatterns={"/myApp/*"})
public class CrosFilter implements Filter {
    private static final Logger logger = LoggerFactory.getLogger(CrosFilter.class);

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException {
        logger.info("对跨域进行过滤操作");
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        // IE8/9需用window.XDomainRequest兼容
        response.setHeader("XDomainRequestAllowed","1");
        String url = request.getRequestURI();
        if(url.endsWith("login")||url.endsWith("regster") ){
            filterChain.doFilter(request,response);
        }
        else{
            if(request.getSession().getAttribute("userAccount")!=null){
                filterChain.doFilter(request,response);
            }
        }


    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }
}
